package kuaidi100;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kuaidi100.sdk.api.QueryTrack;
import com.kuaidi100.sdk.api.QueryTrackMap;
import com.kuaidi100.sdk.core.IBaseClient;
import com.kuaidi100.sdk.request.QueryTrackParam;
import com.kuaidi100.sdk.request.QueryTrackReq;
import com.kuaidi100.sdk.utils.SignUtils;

public class TrackUtil {
    //进入快递100管理后台获取获取授权参数customer和key
    public static final String CUSTOMER = "4901C86DE5CB5E7F2F525C949D961DCA";
    public static final String KEY = "KQRdozUD1440";

    /**
     * 获取实时物流信息
     * @param code 快递公司编号
     * @param num 快递单号
     * @return
     */
    public static String queryTrack(String code,String num){
        QueryTrackReq queryTrackReq = new QueryTrackReq();
        QueryTrackParam queryTrackParam = new QueryTrackParam();

        //设置快递公司的编码
        queryTrackParam.setCom(code);
        //设置快递单号
        queryTrackParam.setNum(num);
        // 开通行政解析功能以及物流轨迹增加物流高级状态名称、状态值并且返回出发、目的及当前城市信息
        queryTrackParam.setResultv2("4");

        String param = JSON.toJSONString(queryTrackParam);
        queryTrackReq.setParam(param);
        queryTrackReq.setCustomer(CUSTOMER);
        queryTrackReq.setSign(SignUtils.querySign(param, KEY, CUSTOMER));
        try {
            //创建查询实时轨迹对象
            IBaseClient baseClient = new QueryTrack();
            String resultStr = baseClient.execute(queryTrackReq).getBody();

            return resultStr;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取带地图的物流轨迹
     * @param code 快递公司编号
     * @param num 快递单号
     * @param from 出发地信息
     * @param to 目的地信息
     * @return
     */
    public static String queryTrackMap(String code,String num,String from,String to){
        QueryTrackReq queryTrackReq = new QueryTrackReq();
        QueryTrackParam queryTrackParam = new QueryTrackParam();

        //设置快递公司的编码
        queryTrackParam.setCom(code);
        //设置快递单号
        queryTrackParam.setNum(num);
        //设置出发地信息
        queryTrackParam.setFrom(from);
        //设置目的地信息
        queryTrackParam.setTo(to);

        String param = JSON.toJSONString(queryTrackParam);

        queryTrackReq.setParam(param);
        queryTrackReq.setCustomer(CUSTOMER);
        queryTrackReq.setSign(SignUtils.querySign(param, KEY, CUSTOMER));
        try {
            //创建查询地图轨迹对象
            IBaseClient baseClient = new QueryTrackMap();
            String resultStr=baseClient.execute(queryTrackReq).getBody();

            return resultStr;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
